package com.yunxi.service.tsp.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.bizToHub.pojo.AssignedInfoVO;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.service.esb.impl.ESBServiceImpl;
import com.yunxi.eai.service.tsp.TspService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/4.
 */
@Service
@Qualifier("tspServiceImpl")
public class TspServiceImpl implements TspService {
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    protected static final Logger logger = LoggerFactory.getLogger(TspServiceImpl.class);

    @Override
    public ApiResult distributeTsp(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        String reqBody = esbVO.getReqBody();
        ApiResult apiResult = null;
       /* if (serviceName.equals(Constant.PMS_TSP_VEHICLE_ASSIGNED_INFO)) {
            //过户接口
            apiResult = saveVehicleAssignedInfo(esbVO);
        } else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }*/
        return apiResult;
    }

    public ApiResult saveVehicleAssignedInfo(EsbVO esbVO) {
        ApiResult apiResult = new ApiResult();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(esbVO.getReqBody());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("TSP发送过户信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        AssignedInfoVO assignedInfoVO = JSON.parseObject(esbVO.getReqBody(), AssignedInfoVO.class);
        try {
            //内部业务


            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            logger.error(e.getMessage(), e);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }
}
